package com.lh.realm;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.lh.constant.Constant;
import com.lh.pojo.SysMenu;
import com.lh.pojo.SysUser;
import com.lh.service.SysMenuService;
import com.lh.service.SysUserService;
import com.lh.util.JwtUtils;
import com.lh.util.RedisUtil;
import org.apache.commons.lang3.StringUtils;
import org.apache.shiro.authc.*;
import org.apache.shiro.authz.AuthorizationInfo;
import org.apache.shiro.authz.SimpleAuthorizationInfo;
import org.apache.shiro.realm.AuthorizingRealm;
import org.apache.shiro.subject.PrincipalCollection;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.List;

public class MyShiroRealm extends AuthorizingRealm {

    @Autowired
    private SysUserService sysUserServiceImpl;
    @Autowired
    private SysMenuService sysMenuServiceImpl;
    @Autowired
    private RedisUtil redisUtil;

    /**
     * 授权方法
     */
    @Override
    protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principalCollection) {
        SimpleAuthorizationInfo simpleAuthorizationInfo = new SimpleAuthorizationInfo();
        String username = JwtUtils.getUsername(principalCollection.toString().replace(Constant.LOGIN_PREFIX, ""));
        SysUser sysUser = sysUserServiceImpl.getOne(new QueryWrapper<SysUser>().eq("username", username));
        if (sysUser != null) {
            // 获取到当前用户的所有菜单
            List<String> perByUser = sysMenuServiceImpl.getPerByUser(sysUser.getId());
            for (String per : perByUser) {
                simpleAuthorizationInfo.addStringPermission(per);
            }
        }
        return simpleAuthorizationInfo;
    }

    @Override
    protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken authenticationToken) throws AuthenticationException {

        BearerToken bearerToken = (BearerToken) authenticationToken;
        // 获取token 并去掉token前缀
        String token = bearerToken.getToken();
        token = token.replace(Constant.LOGIN_PREFIX, "").trim();
        // jwt解析token 获取用户名
        String username = JwtUtils.getUsername(token);
        // 根据用户名查询对象
        SysUser sysUser = sysUserServiceImpl.getOne(new QueryWrapper<SysUser>().eq("username", username));
        if (sysUser == null) {
            throw new AuthenticationException("用户不存在");
        }
        // 校验token是否合法
        if (!JwtUtils.verify(token, username, sysUser.getPassword())) {
            throw new AuthenticationException("用户名密码错误");
        }
        // 校验token是否过期
        String cacheToken = (String) redisUtil.get(Constant.LOGIN_PREFIX + sysUser.getId());
        if (StringUtils.isEmpty(cacheToken) || StrUtil.equals(cacheToken, sysUser.getPassword())) {
            throw new AuthenticationException("token失效");
        }
        // 验证通过刷新token时间
        redisUtil.expire(Constant.LOGIN_PREFIX + sysUser.getId(), Constant.TOKEN_EXPIRE);

        return new SimpleAuthenticationInfo(username, sysUser.getPassword(), "ShiroRealm");
    }
}
